import 'dart:convert';

/// CreateTime 2020/5/17 10:16
/// CreateUser chen
/// ProjectName flutter_elf
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_elf/r.dart';
import 'package:flutter_elf/utils/asset_utils.dart';


class TranslateGeneratePage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TranslateGeneratePageState();
  }
}

class TranslateGeneratePageState extends State<TranslateGeneratePage>{

  String enJson,koJson,zhJson;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initString();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
    ]);
  }


  //语言 <key,value>
  List<LanguageEntity> entities = [];
  Set<String> sets = Set();//keys
  initString()async{
    enJson = await AssetUtils.loadAsset(R.staticDataLanguageStringsEnJson);
    zhJson = await AssetUtils.loadAsset(R.staticDataLanguageStringsZhJson);
    koJson = await AssetUtils.loadAsset(R.staticDataLanguageStringsKoKrJson);
    Map<String,dynamic> enMap = json.decode(enJson);
    Map<String,dynamic> zhMap = json.decode(zhJson);
    Map<String,dynamic> koMap = json.decode(koJson);
    sets.addAll(enMap.keys);
    sets.addAll(zhMap.keys);
    sets.addAll(koMap.keys);
    /*List<LanguageEntity> ens = */
    sets.forEach((f){
      entities.add(LanguageEntity(f,enMap[f] ?? '', zhMap[f] ?? '', koMap[f] ?? ''));
    });
    if(mounted)setState(() {

    });
  }

  bool showEnEmpty = false;//显示英文空白的
  bool showZhEmpty = false;//显示中文空白的
  bool showKoEmpty = false;//显示韩语空白的

  @override
  Widget build(BuildContext context) {
    List<LanguageEntity> data = []..addAll(entities);
    TableRow row1 = TableRow(
        children: [
          Center(child: Text('翻译')),
          Center(child: Text('中文')),
          Center(child: Text('英语')),
          Center(child: Text('韩语')),
        ]
    );
    List<TableRow> rows = [row1];
    if(showEnEmpty){
      data.removeWhere((entity){
        return entity.en.isNotEmpty;
      });
    }
    if(showZhEmpty){
      data.removeWhere((entity){
        return entity.zh.isNotEmpty;
      });
    }
    if(showKoEmpty){
      data.removeWhere((entity){
        return entity.ko.isNotEmpty;
      });
    }
    rows.addAll(data.map<TableRow>((entity){
      return TableRow(
          children:[
            Text('  ' + entity.key),
            GestureDetector(
                onTap: (){
                  showEditDialog(context,data,entity,'zh',entity.zh,(s){
                    entity.zh = s;
                    setState(() {

                    });
                  });
                },
                child: Text('  ' + entity.zh )),
            GestureDetector(
                onTap: (){
                  showEditDialog(context,data,entity,'en',entity.en,(s){
                    entity.en = s;
                    setState(() {

                    });
                  });
                },
                child: Text('  ' + entity.en )),
            GestureDetector(
                onTap: (){
                  showEditDialog(context,data,entity,'ko',entity.ko,(s){
                    entity.ko = s;
                    setState(() {

                    });
                  });
                },
                child: Text('  ' + entity.ko )),
          ]
      );
    }).toList());
    return Scaffold(
      resizeToAvoidBottomPadding: true,
      body: Container(
        margin: EdgeInsets.symmetric(horizontal: 5),
        child: Column(
          children: <Widget>[
            SizedBox(height: 44,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                GestureDetector(
                  onTap: (){
                    showZhEmpty = false;
                    showEnEmpty = false;
                    showZhEmpty = !showZhEmpty;
                    setState(() {});
                  },
                  child: Container(
                    color: Colors.lightBlue,
                    padding: EdgeInsets.all(5),
                    child: Text('显示中文为空',style: TextStyle(color: showZhEmpty ? Colors.red : Colors.white),),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    showZhEmpty = false;
                    showKoEmpty = false;
                    showEnEmpty = !showEnEmpty;
                    setState(() {});
                  },
                  child: Container(
                    color: Colors.lightBlue,
                    padding: EdgeInsets.all(5),
                    child: Text('显示英文为空',style: TextStyle(color: showEnEmpty ? Colors.red : Colors.white),),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    showZhEmpty = false;
                    showEnEmpty = false;
                    showKoEmpty = !showKoEmpty;
                    setState(() {});
                  },
                  child: Container(
                    color: Colors.lightBlue,
                    padding: EdgeInsets.all(5),
                    child: Text('显示韩语为空',style: TextStyle(color: showKoEmpty ? Colors.red : Colors.white),),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    showZhEmpty = false;
                    showEnEmpty = false;
                    showKoEmpty = false;
                    setState(() {});
                  },
                  child: Container(
                    color: Colors.lightBlue,
                    padding: EdgeInsets.all(5),
                    child: Text('清空',style: TextStyle(color: Colors.white),),
                  ),
                ),
              ],
            ),

            Expanded(
              child: SingleChildScrollView(
                child: Table(
                  columnWidths: {
                    0:FractionColumnWidth(0.25),
                    1:FractionColumnWidth(0.25),
                    2:FractionColumnWidth(0.25),
                    3:FractionColumnWidth(0.25),
                  },
                    border: TableBorder.all(
                      color: Colors.grey,
                      width: 1.0,
                      style: BorderStyle.solid,
                    ),
                  children: rows
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }



  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
  }


  showEditDialog(context,List<LanguageEntity> data,LanguageEntity entity,String language,String text,ValueChanged onChange){
    String changed;
    TextEditingController controller = TextEditingController(text: text);
    return showDialog(context: context,barrierDismissible: true,builder: (c){
      return AlertDialog(
        contentPadding: EdgeInsets.all(0),
//        title: Text('修改'),
        content: TextField(
          controller: controller,
//          onChanged: (s){
//            changed = s;
//          },
          textInputAction: TextInputAction.go,
          onSubmitted: (s){
            onChange(s);Navigator.pop(context);
          },
        ),
//        actions: <Widget>[
//          RaisedButton(
//            onPressed: ()=>Navigator.pop(context),
//            child: Text(
//              '取消'
//            ),
//          ),
//          RaisedButton(
//            onPressed: (){
//              onChange(changed);
//              Navigator.pop(context);
//            },
//            child: Text(
//              '确定'
//            ),
//          ),
//        ],
      );
    });
  }

}

class LanguageEntity{
  String en,zh,ko;
  String key;
  LanguageEntity(this.key,this.en, this.zh, this.ko);

  @override
  String toString() {
    // TODO: implement toString
    return
      'en:' + en + ',' +
      'zh:' + zh + ',' +
      'ko:' + ko
    ;
  }

}